
open Ocamlbuild_plugin
open Command

(* Configuration section *)
let seanlib_dir  = "/Users/seanmcl/save/versioned/projects/kepler/code/ocaml-lib/_build"

let have_mpfi = "-DHAVE_MPFI"
let mpfi_lib = "-lmpfi"
let mpfi_libdir = "-L/usr/local/lib"
let mpfi_include = "-I/usr/local/include"

let have_mpfr = "-DHAVE_MPFR"
let mpfr_lib = "-lmpfr"
let mpfr_libdir = "-L/usr/local/lib"
let mpfr_include = "-I/usr/local/include"

let have_gmp = "-DHAVE_GMP"
let gmp_lib = "-lgmp"
let gmp_libdir = "-L/sw/lib"
let gmp_include = "-I/sw/include"

let static = false

(* List of headers *)
let headers =
  ["mpfi.h"; "mpfr.h"; "gmp.h"]

;;

dispatch begin function
| After_rules ->

    (* When one make a C library that use the mpfi with ocamlmklib,
       then issue these flags. *)
    flag ["ocamlmklib"; "c"]
         (S[A mpfi_libdir; A mpfi_lib;
            A mpfr_libdir; A mpfr_lib;
            A gmp_libdir; A gmp_lib]);

    (* When one compile C code using the mpfi *)
    flag ["c"; "compile"; "include_mpfi"]
         (S[A"-ccopt"; A mpfi_include; A"-ccopt"; A have_mpfi;
            A"-ccopt"; A mpfr_include; A"-ccopt"; A have_mpfr;
            A"-ccopt"; A gmp_include; A"-ccopt"; A have_gmp]);

    flag ["link"; "ocaml"; "library"; "use_mpfi"]
         (S[A"-ccopt"; A mpfi_libdir; A"-cclib"; A mpfi_lib;
            A"-ccopt"; A mpfr_libdir; A"-cclib"; A mpfr_lib;
            A"-ccopt"; A gmp_libdir; A"-cclib"; A gmp_lib]);

    (* If `static' is true then every ocaml link in bytecode will add -custom *)
    if static then flag ["link"; "ocaml"; "byte"] (A"-custom");

    (* cryptokit is an ocaml library.
       This will declare use_cryptokit and include_cryptokit *)
    ocaml_lib "mlpfi";

    flag ["link"; "library"; "ocaml"; "byte"; "use_libmpfi"]
         (S[A"-dllib"; A"-lmpfi"; A"-cclib"; A"-lmpfi";
            A"-dllib"; A"-lmpfr"; A"-cclib"; A"-lmpfr";
            A"-dllib"; A"-lgmp"; A"-cclib"; A"-lgmp"]);

    flag ["link"; "library"; "ocaml"; "native"; "use_libmpfi"]
         (S[A"-cclib"; A"-lmpfi";
            A"-cclib"; A"-lmpfr";
            A"-cclib"; A"-lgmp"]);

    flag ["link"; "native"; "use_mlpfi"]
      (S[A"-ccopt"; A"-L."]);

    flag ["link"; "byte"; "use_mlpfi"]
      (S[A"-dllpath"; A"."]);

    (* -------------------------------  Seanlib  -------------------------------- *)
        
    flag ["compile"; "ocaml"; "use_seanlib"]
         (S[A"-I"; A seanlib_dir]);

    flag ["link"; "ocaml"; "byte"; "program"; "use_seanlib"]
         (S[A"-I"; A seanlib_dir; A"lib.cma"]);

    flag ["link"; "ocaml"; "native"; "program"; "use_seanlib"]
         (S[A"-I"; A seanlib_dir; A"lib.cmxa"]);


| _ -> ()
end
